package com.csj.mvvm.xml

import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.csj.mvvm.R
import com.csj.mvvm.utils.LogUtil


object ViewAdapter {

    //如果ImageView同时使用了imageUrl、error，且前者是String，后者是Drawable，就会调用适配器
    //<ImageView app:imageUrl="@{venue.imageUrl}" app:error="@{@drawable/venueError}" />
    @BindingAdapter("imageUrl", "error")
    @JvmStatic
    fun loadImage(view: ImageView, url: String, error: Drawable) {
        Glide.with(view.context).load(url).error(error).into(view)
    }

    //如果你希望设置了任意属性就调用适配器
    @BindingAdapter(value = ["imageUrl", "error", "placeholder"], requireAll = false)
    @JvmStatic
    fun setImageUrl(imageView: ImageView, url: String?, error: Drawable?, placeHolder: Drawable?) {
        if (url == null) {
            imageView.setImageDrawable(
                placeHolder ?: ContextCompat.getDrawable(
                    imageView.context, R.drawable.drawable_default
                )
            )
        } else {
            Glide.with(imageView.context).load(url)
                .placeholder(
                    placeHolder ?: ContextCompat.getDrawable(
                        imageView.context, R.drawable.drawable_default
                    )
                ).error(
                    error ?: ContextCompat.getDrawable(
                        imageView.context, R.drawable.drawable_default
                    )
                ).into(imageView)
            //ImageActivity
//            Glide.with(imageView.context)
//                .asBitmap()
//                .load(url)
//                .placeholder(R.drawable.drawable_default)
//                .error(R.drawable.drawable_default)
//                .into(object : CustomTarget<Bitmap?>() {
//
//                    override fun onResourceReady(
//                        resource: Bitmap,
//                        transition: Transition<in Bitmap?>?
//                    ) {
//                        // 设置图片
////                        return resource
//                        imageView.setImageBitmap(resource)
//                    }
//
//                    override fun onLoadCleared(placeholder: Drawable?) {
//                    }
//                })
        }

    }

    //app:color_checked="@{@color/black}"
    //app:color_normal="@{@color/color_999999}"
    @BindingAdapter(
        value = ["color_pressed", "color_selected", "color_checked", "color_focused", "color_normal"],
        requireAll = false
    )
    @JvmStatic
    fun adaptTextColor(
        view: View,
        state_pressed: Int,
        state_selected: Int,
        state_checked: Int,
        state_focused: Int,
        color_normal: Int
    ) {

        try {
            var sizes = 0
            if (state_pressed != 0) {
                ++sizes
            }
            if (state_selected != 0) {
                ++sizes
            }
            if (state_checked != 0) {
                ++sizes
            }
            if (state_focused != 0) {
                ++sizes
            }
            if (color_normal != 0) {
                ++sizes
            }
            var count = 0
            val states = arrayOfNulls<IntArray>(sizes)
            val colors = IntArray(sizes)
            if (state_pressed != 0) {
                colors[count] = state_pressed
                states[count] = intArrayOf(android.R.attr.state_pressed)
                ++count
            }
            if (state_selected != 0) {
                colors[count] = state_selected
                states[count] = intArrayOf(android.R.attr.state_selected)
                ++count
            }
            if (state_checked != 0) {
                colors[count] = state_checked
                states[count] = intArrayOf(android.R.attr.state_checked)
                ++count
            }
            if (state_focused != 0) {
                colors[count] = state_focused
                states[count] = intArrayOf(android.R.attr.state_focused)
                ++count
            }
            if (color_normal != 0) {
                colors[count] = color_normal
                states[count] = intArrayOf()
            }
            (view as TextView).setTextColor(ColorStateList(states, colors))
        } catch (e: Exception) {
            LogUtil.e("adaptTextColor error = " + e.message)
        }
    }

}